/**  
 * All rights Reserved, Designed By www.richfit.com
 * @Title:  DataSourceSwitchAspect.java   
 * @Package cn.eonshine.base.config.mulitdb   
 * @Description:    TODO
 * @author: lanjf2000
 * @date:   2018-10-15 18:59:00
 * @version V1.0 
 * @Copyright: 2018 www.richfit.com Inc. All rights reserved. 
 */
package com.richfit.cuba.web.config.db.mulitdb.aop;

import java.util.Objects;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.richfit.cuba.web.config.db.mulitdb.DataSourceContextHolder;
import com.richfit.cuba.web.config.db.mulitdb.DataSourceEnum;
import com.richfit.cuba.web.config.db.mulitdb.DataSourceFlag;
import com.richfit.cuba.web.config.db.mulitdb.DataSourceSwitch;

import lombok.extern.slf4j.Slf4j;

/**   
 * @ClassName:  DataSourceSwitchAspect   
 * @Description:TODO
 * @author: lanjf2000
 * @date:   2018-10-15 18:59:00
 *     
 * @Copyright: 2018 www.richfit.com Inc. All rights reserved. 
 */
@Component
@Aspect
@Order(-100) //这是为了保证AOP在事务注解之前生效,Order的值越小,优先级越高
@Slf4j
public class DataSourceSwitchAspect {
	
	@Pointcut("execution(* com.richfit.cuba.modular.lsms..*.*(..))")
    private void db1Aspect() {
    }

    @Pointcut("execution(* com.richfit.cuba.modular.tp..*.*(..))")
    private void db2Aspect() {
    }

    @Before( "db1Aspect()" )
    public void db1() {
        log.info("Change to db1 (lsms) DataSource...");
        DataSourceContextHolder.setDataSource(DataSourceEnum.DB1.getValue());
    }

    @Before("db2Aspect()" )
    public void db2 () {
        log.info("Change to db2 (inf) DataSource...");
        DataSourceContextHolder.setDataSource(DataSourceEnum.DB2.getValue());
    }
    
}
